package com.kaibes.core.base.thread;

public interface CountingThread extends RunThread {
    int getTaskCount();
    long getInterval();
    int getCount();
    void setCount(int count);
    
    default void run() {
        setCount(0);
        setRunning();
        long taskCount = getTaskCount();
        while (isRunning()) {
            doWork();
            if (getInterval() > 0) {
                try {
                    Thread.sleep(getInterval());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            setCount(getCount()+1);
            if (getCount() >= taskCount) {
                stop();
                doResult();
            }
        }
    }
    default void doResult() {};
}
